package com.sevencar.activity;


import java.util.ArrayList;

import com.sevencar.R;
import com.sevencar.activity.cityselect.CityPickActivity;
import com.sevencar.peterview.ImageData;
import com.sevencar.peterview.MyFragment;
import com.sevencar.peterview.TestActivity;

import me.maxwin.view.AbOnItemClickListener;
import me.maxwin.view.AbSlidingPlayView;
import me.maxwin.view.XListView.IXListViewListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

public class HomePageFragment extends Fragment implements IXListViewListener {

	private AbSlidingPlayView viewPager;
	
	ImageView back_img = null;
	ImageView iv_product = null;
	
	ImageView img_right;
	private ArrayList<View> allListView;
	private int[] resId = { R.drawable.tu1, R.drawable.menu_viewpager_1, R.drawable.menu_viewpager_2, R.drawable.menu_viewpager_3, R.drawable.menu_viewpager_4, R.drawable.menu_viewpager_5 };

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_home, container, false);
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initViews(view);
		initViewPager();
		
		
		ArrayList<ImageData> result = new ArrayList<ImageData>();
		for (int i = 1; i <= 20; i++) {
			result.add(new ImageData("Title" + i, i + ".jpg"));
		}
        
        Fragment f = MyFragment.newInstance(result);
		
		getActivity().getSupportFragmentManager().beginTransaction().add(R.id.container_h,f).commit();
		
		
		
		
		
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initDisplay();
	}
	
	private void initViews(View view) {
//		iv_product = (ImageView) view.findViewById(R.id.iv_product);
		back_img = (ImageView) view.findViewById(R.id.back_img);
		img_right = (ImageView) view.findViewById(R.id.right_img);
		viewPager = (AbSlidingPlayView) view.findViewById(R.id.viewPager_menu);
		viewPager.setPlayType(1);
		viewPager.setSleepTime(3000);
	}
	
	private void initDisplay() {
//		mMsgTv.setText(mMsgName + "");
		back_img.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
//				intent.setClass(getActivity(), MipcaActivityCapture.class);
				intent.setClass(getActivity(), CityPickActivity.class);
//				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
		});
		img_right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(getActivity(), TestActivity.class);
				startActivity(intent);
			}
		});
//		iv_product.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Intent intent = new Intent();
//				intent.setClass(getActivity(), ProductActivity.class);
//				startActivity(intent);
//			}
//		});
	}
	
	@Override
	public void onRefresh() {
		
	}

	@Override
	public void onLoadMore() {
		
	}
	
	private void initViewPager() {

		if (allListView != null) {
			allListView.clear();
			allListView = null;
		}
		allListView = new ArrayList<View>();

		for (int i = 0; i < resId.length; i++) {
			View view = LayoutInflater.from(getActivity()).inflate(R.layout.pic_item, null);
			ImageView imageView = (ImageView) view.findViewById(R.id.pic_item);
			imageView.setImageResource(resId[i]);
			allListView.add(view);
		}
		
		
		viewPager.addViews(allListView);
		viewPager.startPlay();
		viewPager.setOnItemClickListener(new AbOnItemClickListener() {
			@Override
			public void onClick(int position) {
//				Intent intent = new Intent(getActivity(), BabyActivity.class);
//				startActivity(intent);
			}
		});
	}

}
